/**
  ******************************************************************************
  * @file    User/i2c1_bus.h
  * @date    18-Mar-2018
  * @brief   all the functions prototypes for simulation i2c1
  ******************************************************************************
  */

#ifndef __I2C1_BUS_H
#define __I2C1_BUS_H

#include "stm32f10x.h"
#include "systick_delay.h"

#define I2C1_SCL_PORT				GPIOB
#define I2C1_SCL_PIN				GPIO_Pin_6
#define I2C1_SDA_PORT				GPIOB
#define I2C1_SDA_PIN				GPIO_Pin_7
#define I2C1_GPIO_CLK				RCC_APB2Periph_GPIOB
#define I2C1_GPIO_CLK_CMD		RCC_APB2PeriphClockCmd

#define I2C1_SCL(x)		GPIO_WriteBit(I2C1_SCL_PORT, I2C1_SCL_PIN, (BitAction)(x))
#define I2C1_SDA(x)		GPIO_WriteBit(I2C1_SDA_PORT, I2C1_SDA_PIN, (BitAction)(x))

#define I2C1_SCL_LOW	GPIO_ResetBits(I2C1_SCL_PORT, I2C1_SCL_PIN)
#define I2C1_SCL_HIGH	GPIO_SetBits(I2C1_SCL_PORT, I2C1_SCL_PIN)
#define I2C1_SDA_LOW	GPIO_ResetBits(I2C1_SDA_PORT, I2C1_SDA_PIN)
#define I2C1_SDA_HIGH	GPIO_SetBits(I2C1_SDA_PORT, I2C1_SDA_PIN)

#define I2C1_SDA_READ	(GPIO_ReadInputDataBit(I2C1_SDA_PORT, I2C1_SDA_PIN) & 0x01)
#define I2C1_SDAin																			\
{																												\
	i2c1GPIOInitStruct.GPIO_Pin = I2C1_SDA_PIN;						\
	i2c1GPIOInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;	\
	GPIO_Init(I2C1_SDA_PORT, &i2c1GPIOInitStruct);				\
}
#define I2C1_SDAout																	\
{																										\
	i2c1GPIOInitStruct.GPIO_Pin = I2C1_SDA_PIN;				\
	i2c1GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_OD;	\
	GPIO_Init(I2C1_SDA_PORT, &i2c1GPIOInitStruct);		\
}

#define I2C1_DELAY			3
#define ACK_SIGNAL			0
#define NACK_SIGNAL			1

void I2c1_Bus_Init(void);
static void I2C1_Init(uint32_t clkSpeed);
static void I2C1_Start(void);
static void I2C1_Stop(void);
static void I2C1_SendACK(uint8_t ack);
static uint8_t I2C1_WaitACK(void);
static void I2C1_SendByte(uint8_t data);
static uint8_t I2C1_RecvByte(void);
uint8_t I2C1_WriteData(uint8_t devAddress, uint8_t regAddress, uint8_t *data, uint16_t length);
uint8_t I2C1_ReadData_8B_Addr(uint8_t devAddress, uint8_t regAddress, uint8_t *data, uint16_t length);
uint8_t I2C1_ReadData_16B_Addr(uint8_t devAddress, uint16_t command, uint8_t *data, uint16_t length);
uint8_t I2C1_ReadData_8B_Addr_NHMM(uint8_t devAddress, uint8_t regAddress, uint8_t *data, uint16_t length);
uint8_t I2C1_CheckDev(uint8_t devAddress);

#endif
